<template>
  <div class="container">
    <el-dialog title="修改标签" :visible.sync="dialogVisible" @close="changeShowFlag" width="30%">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px">
        <!-- 第一行 -->
        <el-row>
          <el-col :span="24">
            <el-form-item label="所属学科">
              <el-select v-model="ruleForm.subjectID" placeholder="请选择">
                <el-option
                  v-for="item in subjectList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <!-- 第二行 -->
        <el-row>
          <el-col :span="22">
            <el-form-item label="标签名称" prop="tagName">
              <el-input v-model="ruleForm.tagName"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm('ruleForm')">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { simple } from "@/api/hmmm/subjects"
import { update } from "@/api/hmmm/tags"
export default {
  props: {
    subjectID: {
      type: Number
    },
    tagName: {
      type: String
    },
    id: {
      type: Number
    }
  },
  data () {
    return {
      // 表单数据
      ruleForm: {
        tagName: '',
        subjectID: ''
      },

      subjectList: [],
      rules: {
        tagName: [
          { required: true, message: '请输入标签名称', trigger: 'blur' },
          { min: 1, max: 10, message: '长度在 1 到 10 个字符', trigger: 'blur' }
        ],
      },
      dialogVisible: false
    };
  },
  mounted () {
    this.getSubjectList()
    this.ruleForm.tagName = this.tagName
    this.ruleForm.subjectID = this.subjectID

  },
  methods: {
    // 显示弹窗
    showDialog () {
      this.dialogVisible = true
    },
    //复原父组件的显示对话框绑定数据
    changeShowFlag () {
      this.$emit('changeDialogTagsAdd', false)
    },
    // 渲染学科下拉框
    async getSubjectList () {
      try {
        const res = await simple()
        // console.log(res);
        this.subjectList = res.data
      } catch (err) {
        console.log(err);
      }
    },
    // 完成表单验证修改
    submitForm (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.updateData()

          this.useRefresh()
        } else {
          return false;
        }
      })
    },
    // 调用修改接口
    async updateData () {
      try {
        await update({
          id: this.id,
          subjectID: this.ruleForm.subjectID,
          tagName: this.ruleForm.tagName
        })
        this.$message({
          message: '修改标签成功',
          type: 'success'
        })
        this.useRefresh()
        // this.$router.push({
        //   path: '/subjects/tags'
        // })
      }
      catch (err) {
        this.$message({
          message: '修改标签失败',
          type: 'warning'
        })
      }
    },
    //调用父组件刷新列表方法
    useRefresh () {
      window.location.reload()
      // console.log(this.$parent)
      // this.$parent.getTagsList()
      // this.$emit('getTagsList')
    }
  }
};
</script>

<style scoped lang='less'>
.container {
  /deep/.el-input {
    width: 245px;
  }
}
</style>
